import com.sun.source.tree.Tree;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class brainytree {

    class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    public int preIndex;
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        return buildTreeChild(preorder, inorder, 0, inorder.length - 1);

    }

    public TreeNode buildTreeChild(int[] preorder,int[] inorder, int inbegin, int inend) {
        if (inbegin > inend) {
            return null;
        }
        TreeNode root = new TreeNode(preorder[preIndex]);
        int findIndex = Find(inorder, inbegin, inend,preorder[preIndex]);
        preIndex++;
        root.left = buildTreeChild(preorder, inorder, inbegin, findIndex - 1);
        root.right = buildTreeChild(preorder, inorder, findIndex + 1, inend);
        return root;



    }
    public int Find( int[] inorder, int inbegin, int inend, int p) {
        for (int i = inbegin; i <= inend; i++) {
            if (inorder[i] == p) {
                return i;
            }
        }
        return -1;
    }

    public void preinodernor(TreeNode root) {
        if (root == null) {
            return;
        }
        Stack<TreeNode> s = new Stack<>();
        TreeNode p = root;
        while (p != null || !s.isEmpty()) {
            while (p != null) {
                s.push(p);
                System.out.print(p.val + " ");
                p = p.left;
            }
            TreeNode t = s.pop();
            p = t.right;
        }
    }

    public void preorder(TreeNode root) {
        if (root == null) {
            return;
        }
        Stack<TreeNode> s = new Stack<>();
        TreeNode p = root;
        while (!s.isEmpty() || p != null) {
            while (p != null) {
                s.push(p);
                System.out.print(p.val + " ");
                p = p.left;
            }
            TreeNode t = s.pop();
            p = t.right;
        }
    }

    public void postorder(TreeNode root) {
        if (root == null) {
            return;
        }
        TreeNode prev = null;
        Stack<TreeNode> s = new Stack<>();
        TreeNode p = root;
        while (p != null || !s.isEmpty()) {
            while (p != null) {
                s.push(p);
                p = p.left;
            }
            TreeNode t = s.peek();
            if (t.right == null || t.right == prev) {
                System.out.print(t.val + " ");
                s.pop();
                prev = t;

            } else {
                p = t.right;
            }
        }

    }


    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> l = new ArrayList<>();
        if (root == null) {
            return l;
        }
        Stack<TreeNode> s = new Stack<>();
        TreeNode p = root;
        TreeNode prev = null;
        while (p != null || !s.isEmpty()) {
            while (p != null) {
                s.push(p);
                p = p.left;
            }
            TreeNode t = s.peek();
            if (t.right == null || t.right == prev) {
                l.add(t.val);
                s.pop();
                prev = t;

            } else {
                p = t.right;
            }
        }
        return l;

    }
}
